package com.projecti18n;
 
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
 
/**
 * Says "Hi" to the user.
 *
 */
@Mojo( name = "excel")
public class GreetingMojo extends AbstractMojo
{
	@Parameter( defaultValue = "${project.resources}", property = "resources", required = true )
    private List<Resource> resources;
	
	@Parameter( defaultValue = "${project.build.outputDirectory}", property = "outputDirectory", required = true )
    private File outputDirectory;
	
	@Parameter(name ="basename" , defaultValue = "i18n/messages")
	private String basename;
	@Parameter(name ="messageConfig" , defaultValue = "i18n.xlsx")
	private String messageConfig;
	
	private String messageName;
	private File messageResourcesDir;
	private File messageTargetDir;
	
    public void execute() throws MojoExecutionException
    {
    	File file = new File(outputDirectory,basename);
    	messageName = file.getName();
    	messageTargetDir = file.getParentFile();
    	File messageConfigFile=null;
    	for (Resource resource : resources) {
    		messageConfigFile = new File(resource.getDirectory(), messageConfig);
    		messageResourcesDir = new File(resource.getDirectory(), basename).getParentFile();
    		if(messageConfigFile.exists()) {
    			break;
    		}
    		messageConfigFile = new File(new File(resource.getDirectory(),basename).getParentFile(), messageConfig);
    		if(messageConfigFile.exists()) {
    			break;
    		}
		}
    	if(!messageConfigFile.exists()) {
			throw new MojoExecutionException("messageConfig: "+messageConfig+", 未找到");
		}
    	
    	ExcelReader reader = ExcelUtil.getReader(FileUtil.file(messageConfigFile),messageName);
    	List<List<Object>> readAll = reader.read();
    	Map<String, Props> propsMap=new HashMap<>();
    	for (int i = 1; i < readAll.size(); i++) {
    		List<Object> line = readAll.get(i);
    		String key = line.get(0)+"";
			for (int r = 1; r < line.size(); r++) {
				String locale = readAll.get(0).get(r)+"";
				Props props = propsMap.get(locale);
				if(props == null) {
					props = new Props();
					propsMap.put(locale, props);
				}
				props.setProperty(key, line.get(r)+"");
			}
		}
    	for (String key : propsMap.keySet()) {
    		if(StrUtil.isBlank(key)) {
    			propsMap.get(key).store(new File(messageResourcesDir,messageName+".properties").getAbsolutePath());
    			propsMap.get(key).store(new File(messageTargetDir,messageName+".properties").getAbsolutePath());
    		}else {
    			propsMap.get(key).store(new File(messageResourcesDir,messageName+"_"+key+".properties").getAbsolutePath());
    			propsMap.get(key).store(new File(messageTargetDir,messageName+"_"+key+".properties").getAbsolutePath());	
    		}
		}
    	
    }
}